package app.controllers.mobileweb;

import app.Const;
import app.dtos.CurrencyToIntegral;
import app.interceptors.MobileWxMemberInterceptor;
import app.models.game.SignRule;
import app.models.member.Member;
import app.models.member.MemberCurrencyRecord;
import app.models.sys.Currency;
import com.alibaba.fastjson.JSON;
import com.github.sog.controller.AjaxMessage;
import com.github.sog.controller.BasicController;
import com.github.sog.plugin.sqlinxml.SqlKit;
import com.google.common.base.Strings;
import com.jfinal.aop.Before;

import java.util.List;

/**
 * <p>
 * .
 * </p>
 *
 * @author Jerry Ou
 * @version 1.0 2014-02-12 13:41
 * @since JDK 1.6
 */
@Before(MobileWxMemberInterceptor.class)
public class CurrencyController extends BasicController {

    private static final String EXCHANGE_TO_INTEGRAL = "兑换中南币";
    private static final String EXCHANGE_FROM_CURRENCY = "中南币兑换";

    public void index() {
        long mid = getParaToLong(Const.MEMBER_PARAM, 0l);
        Member member = Member.dao.findById(mid);
        SignRule signRule = SignRule.dao.findFirst(SqlKit.sql("signrule.list"));
        Currency currency = Currency.dao.findFirst(SqlKit.sql("currency.list"));
        if (currency == null) {
            currency = new Currency();
        }
        String json = currency.getStr("rule");
        CurrencyToIntegral cti = new CurrencyToIntegral();
        if (!Strings.isNullOrEmpty(json)) {
            cti = JSON.parseObject(json, CurrencyToIntegral.class);
        }
        setAttr("cti", cti);
        setAttr("member", member);
        setAttr("signRule", signRule);
        render("index.ftl");
    }

    public void exchange() {
        Currency currency = Currency.dao.findFirst(SqlKit.sql("currency.list"));
        if (currency == null) {
            currency = new Currency();
        }
        String json = currency.getStr("rule");
        CurrencyToIntegral cti = new CurrencyToIntegral();
        if (!Strings.isNullOrEmpty(json)) {
            cti = JSON.parseObject(json, CurrencyToIntegral.class);
        }
        setAttr("cti", cti);
        long mid = getParaToLong(Const.MEMBER_PARAM, 0l);
        Member member = Member.dao.findById(mid);
        setAttr("member", member);
        render("exchange.ftl");
    }

    private double parseDouble(String str) {
        double val = 0;
        if (!Strings.isNullOrEmpty(str)) {
            val = Double.parseDouble(str);
        }
        return val;
    }

    public void exchangeSave() {
        long mid = getParaToLong(Const.MEMBER_PARAM, 0l);
        String currencyStr = getPara("currency");
        String integralStr = getPara("integral");
        double currency = parseDouble(currencyStr);
        double integral = parseDouble(integralStr);
        if (currency == 0 || integral == 0) {
            renderJson(AjaxMessage.error("输入有误"));
            return;
        }

        Member member = Member.dao.findById(mid);
        if (member == null) {
            renderJson(AjaxMessage.error("没有该会员"));
            return;
        }
        if (Strings.isNullOrEmpty(member.getStr("phone"))) {
            renderJson(AjaxMessage.error("请先绑定会员"));
            return;
        }

        Currency currencyRule = Currency.dao.findFirst(SqlKit.sql("currency.list"));
        if (currencyRule == null) {
            renderJson(AjaxMessage.error("没有兑换规则"));
            return;
        }
        String json = currencyRule.getStr("rule");

        if (Strings.isNullOrEmpty(json)) {
            renderJson(AjaxMessage.error("没有兑换规则"));
            return;
        }
        CurrencyToIntegral cti = JSON.parseObject(json, CurrencyToIntegral.class);

        double curCurr = member.getDouble("currency");
        if (curCurr < currency) {
            renderJson(AjaxMessage.error("当前中南币不够兑换"));
            return;
        }

        double scale = cti.integral/cti.currency;
        double tar = currency * scale;
        if (tar != integral) {
            renderJson(AjaxMessage.error("兑换比例计算不等于兑换积分"));
            return;
        }

        Member.dao.currencyExchangeIntegral(member, currency, integral);

        //Member.dao.optCurrency(mid, false, currency, EXCHANGE_TO_INTEGRAL);
        //Member.dao.optIntegral(mid, true, integral, EXCHANGE_FROM_CURRENCY);


        renderJson(AjaxMessage.ok("兑换成功"));
    }


    public void record() {
        long mid =  getParaToLong(Const.MEMBER_PARAM, 0l);
        List<MemberCurrencyRecord> records = MemberCurrencyRecord.dao.findByMember(mid);
        setAttr("records", records);
        render("record.ftl");
    }

}
